<?php

namespace App\Http\Controllers\Customer;

use Illuminate\Http\Request;
use App\Http\Controllers\Base\BaseController;
use App\Http\Query\GoodsDetailQuery;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;

class JapanSizeExchangeController extends BaseController
{


    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function size_exchange(Request $request)
    {
        $getParam = $request->all();
        $map = [];
        $map['uo.state'] = 0;
        $map['uod.update_state'] = 0;
        if (!empty($getParam['order_id'])) {
            $map['uo.order_id'] = $getParam['order_id'];
        }
        $list = DB::table('user_order as uo')
            ->select(DB::raw('uo.order_id,uo.shop_id,uod.goods_price,uo.create_time,uo.order_id,uod.detail_id,uo.order_id,uo.create_time,uo.send_goods_time,uod.size,uod.color,sm.storehouse_name,gm.goods_id,uod.size,gm.goods_name,uod.goods_num,um.user_name,uo.deliver_state,uod.model_id'))
            ->leftJoin('user_order_detailed as uod', 'uo.order_id', '=', 'uod.order_id')
            ->leftJoin('goods_master as gm', 'uod.goods_id', '=', 'gm.goods_id')
            ->leftJoin('goods_model as gmm', 'gm.goods_id', '=', DB::raw('gmm.goods_id and gmm.goods_model_id = uod.model_id'))
            ->leftJoin('user_master as um', 'um.user_id', '=', 'uo.user_id')
            ->leftJoin('storehouse_master as sm', 'uod.warehouse_id', '=', 'sm.storehouse_id')
            ->where($map)
            ->get();
        foreach ($list as $key => $value) {
            $map = [];
            $map['goods_id'] = $value->goods_id;
            $map['model_size'] = $value->size;
            $temp = DB::table('storehouse_detailed_size')
                ->first();
            if (empty($value->send_goods_time)) {
                if (empty($temp) || $temp->num == 0) {
                    $value->send_goods_time = '入荷予定';
                } else {
                    $value->send_goods_time = '出荷待機中';
                }
            }
            $tempSize = DB::table('goods_model')
                ->where('goods_id', '=', $value->goods_id)
                ->get()->map(function ($value) {
                    return (array)$value;
                })->toArray();
            foreach ($tempSize as $ks=>$ts){
                $tempSize[$ks]['size_name'] = fun_size($ts['size']);
                $tempSize[$ks]['color_name'] = fun_colors($ts['color']);
            }
            $list[$key]->model_info = $tempSize;
        }
        $mo = new \App\Model\OrderModel();
        return view('customer.size_exchange', array(
            'list' => $list,
            'size' => $mo::size(),
            'param' => $getParam,
        )); // 返回视图
    }

    public function change_order(Request $request)
    {
        $user_id = Cookie::get('user_id');
        $user_name = Cookie::get('user_name');

        $corp_id = Cookie::get('corp_id');

        $paramData = $request->input('data');

        $orderData['order_id'] = 'NOF.'.rand(111111,999999);

        $shopInfo = DB::table('shop_master')->where(['shop_id' => $paramData['shop_id']])->first();

        $orderData['user_id'] = $user_id;
        $orderData['user_name'] = $user_name;
        $orderData['money'] = $paramData['goods_num'] * $paramData['goods_price'];
        $orderData['consignation_date'] = empty($paramData['weituo_date']) ? toDate(time()) : $paramData['weituo_date'];
        $orderData['create_time'] = toDate(time());
        $orderData['send_goods_time'] = empty($paramData['goods_date']) ? toDate(time()) : $paramData['goods_date'];

        $orderData['name'] = $shopInfo->shop_name;
        $orderData['shop_id'] = $shopInfo->shop_id;
        $orderData['corp_id'] = $corp_id;

        $orderData['add'] = $shopInfo->shop_adress;
        $orderData['add_alias'] = $shopInfo->shop_biru_name;
        $orderData['tel'] = $shopInfo->shop_telephone_number;
        $orderData['postal_code'] = $shopInfo->shop_postcode;
        $orderData['fax'] = $shopInfo->shop_fax_number;
        $orderData['type'] = 2;
        DB::beginTransaction();
        $ret = DB::table('user_order')->insert($orderData);

        if ($ret != false) {
            $detailData = [];
            $gData['order_id'] = $orderData['order_id'];
            $gData['goods_id'] = $paramData['goods_id'];
            $gData['goods_num'] = $paramData['goods_num'];
            $gData['goods_name'] = $paramData['goods_name'];
            $gData['goods_price'] = $paramData['goods_price'];
            $gData['old_detail_id'] = $paramData['detail_id'];
            $gData['size'] = $paramData['size_name'];
            $gData['color'] = $paramData['color'];
            $gData['model_id'] = $paramData['model_id'];
            $gData['deliver_time'] = toDate(time());
            $gData['create_time'] = toDate(time());
            $detailData[] = $gData;
            if (empty($detailData)) {
                DB::rollBack();
                return $this::showReturnCode(1033, [], trans('symessages.ajax_data.detail_data'));
            }

            $ret2 = DB::table('user_order_detailed')->insert($detailData);
            DB::table('user_order_detailed')->where(['order_id'=>$paramData['order_id'],'goods_id'=>$paramData['goods_id'],'model_id'=>$paramData['model_id']])->update(['update_state'=>1]);

            if ($ret2 === true) {
                DB::commit();
                return $this::showReturnCode(1001, [], trans('symessages.ajax_data.insert_success'));
            }
        } else {
            return $this::showReturnCode(1031);
        }

    }
}

